/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;


import java.net.URI;

import step.Node;
import snobject.snCommand;

import http.Client;
import http.Status;

@SuppressWarnings("serial")
public class EvaluationFailure extends Exception 
{
    public EvaluationFailure(Node Node, String Reason) 
    {
	node = Node;
	reason = Reason;
    }
    public EvaluationFailure(Node Node, http.Status Status) 
    {
	node = Node;
	reason = "Failed because of an HTTP issue: " + Status.GetStatusLine();
    }
    public void PrintErrorReport() {
	System.out.println("Evaluation error for node " + 
			   "[" + node.GetID() + "]" + 
			   ", cause: [" + reason + "]");
    }

    public void NotifyServiceDispatcher() throws Status {
	System.err.println("Don't yet know how to contact the service dispatcher...");
	URI uriPathToSSD = sxe.Server.GetServiceDispatcher(); 
	if (uriPathToSSD != null) {
		URI uri;
		try { 
			uri = new URI(uriPathToSSD.toString() + "node/" + node.GetID() + "/");
		} catch (java.net.URISyntaxException e) {
			throw new http.ServerErrorStatus
				(500, 
				 "Internal Server Error",
				 "Service Dispatcher URI [" +
				 uriPathToSSD.toString() + 
				 "] is malformed!");
		}
		try {
			Client.POST(uri, snCommand.EXFAIL);
		} catch (java.io.IOException ex) { /* ignore */
			throw new http.ServerErrorStatus
				(502,
				 "Bad Gateway",
				 "Failed contacting SD");
		}
	}
    }

    public String getMessage() { return reason; }
    protected Node node;
    protected String reason;
}
